#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int n = nums.size();
        int sum = 0;
        for (auto e : nums) sum += e;
        int tgt = sum - x;
        if (tgt < 0)return -1;
        if (!tgt)return n;
        int cur = 0;
        int len = 0;
        for (int left = 0, right = 0; right < n; right++)
        {
            cur += nums[right];
            while (cur > tgt)
                cur -= nums[left++];
            if (cur == tgt)
                len = max(right - left + 1, len);
        }
        if (len)
            return n - len;
        return -1;
    }
};